import time
from telnetlib import EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import ChatDriver


class ContactList:
    url=""
    driver=""
    def __init__(self):
        self.url="http://127.0.0.1:8080/client.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)
    def InforTestByLogin(self):
        # 检查元素
        #输入框
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > textarea")
        #发送按钮
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.ctrl > button")

        #登录状态下，正常发送信息
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > textarea").send_keys("我是张三，晚上不辞")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.ctrl > button").click()
        actal=self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.message-show > div:nth-child(52) > div > p").text
        # 添加屏幕截图
        ChatDriver.getScreeShot()
        assert actal == "我是张三，晚上不辞"

        #登录状态下，异常发送信息
        #输入信息不点击
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > textarea").send_keys("我是张三，晚上不辞")
        #空白信息点击
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > textarea").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.client-container > div > div.right > div.ctrl > button").click()

    #未登录状态
    def InforTestByNotLogin(self):
        newurl = "http://127.0.0.1:8080/client.html"
        self.driver.get(newurl)
        alert = self.driver.switch_to.alert
        text = alert.text
        assert text == "当前用户未登录!"
        # 添加屏幕截图
        ChatDriver.getScreeShot()
        alert.accept()
        #回到登录界面
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 切换到警告框
        # 等待警告框出现
        try:
            WebDriverWait(self.driver, 10).until(EC.alert_is_present())
            alert = self.driver.switch_to.alert
        except Exception as e:
            print(f"警告框未出现或已关闭: {e}")
        alert = self.driver.switch_to.alert
        alert.accept()